﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace mstdump_Analyzer
{
    class RunningConfig
    {
        private ArrayList items = new ArrayList();
        private string activeConfigFile = "";
        private bool KEEP_RUNNING; 
        private string OUTPUT_DIR_PATH;
        //
        //----------------------------------------------------
        //
        public RunningConfig(ref bool KEEP_RUNNING, ref string OUTPUT_DIR_PATH)
        {
            this.KEEP_RUNNING = KEEP_RUNNING;   
            this.OUTPUT_DIR_PATH = OUTPUT_DIR_PATH;
        }
        //
        //----------------------------------------------------
        //
        public ArrayList getRunningConfig()
        {
            return items;
        }
        //
        //----------------------------------------------------
        //
        public string getActiveConfigFile()
        {
            return activeConfigFile;
        }
        //
        //----------------------------------------------------
        //
        public void scanInfo()
        {
            string path = Methods.FindFileRec(OUTPUT_DIR_PATH, "running-config.txt");
            if (path != null)
            {
                scanRunningConfig(path);
            }
        }
        //
        //----------------------------------------------------
        //
        public void scanRunningConfig(string srcFilePath)
        {
            if (srcFilePath == null)
                return;
            StreamReader sr = new StreamReader(srcFilePath);
            try
            {
                string line = sr.ReadLine();
                if (line.Contains("An internal error occurred."))
                {
                    items.Add(new Item(line, line));
                }
                line = sr.ReadLine();
                if (line != null && line.Contains("Running database"))
                {
                    activeConfigFile = line.Substring(line.IndexOf("\""));
                    activeConfigFile = Regex.Replace(activeConfigFile, "\"", "");
                }
                // skip the first four lines
                sr.ReadLine(); sr.ReadLine();

                Item tmpItem = new Item();
                while (KEEP_RUNNING && sr.Peek() >= 0)
                {
                    try
                    {
                        // read line
                        line = sr.ReadLine();
                        if (line.Contains("An internal error occurred."))
                        {
                            items.Add(new Item(line,line));
                        }
                        if (line.Contains("##") && Regex.Matches(line, "##").Count == 1)
                        {
                            if (tmpItem.itemName.Equals(""))
                            {
                                if (line.Length < 3)
                                {
                                    line = sr.ReadLine(); // skip line
                                }
                                tmpItem.itemName = Regex.Split(line, "## ")[1];
                                tmpItem.content = tmpItem.itemName + "\r\n\r\n";
                                line = sr.ReadLine(); // skip line
                            }
                            else
                            {
                                items.Add(tmpItem);
                                tmpItem = new Item();
                            }
                        }
                        else
                        {
                            tmpItem.content += line + "\r\n";
                        }
                    }
                    catch
                    {
                    }
                }
                // add the last one
                if (items != null && tmpItem != null)
                {
                    items.Add(tmpItem);
                }
            }
            catch
            { }
            sr.Dispose();
        }
        //
        //----------------------------------------------------
        //
    }
}
